/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Desarrollo.Soft.Hibernate.Control;

import java.util.List;
import org.hibernate.Session;
import Desarrollo.Soft.Hibernate.Util.HibernateUtil;
import org.hibernate.Criteria;

/**
 *
 * @author r0ck3r
 */
public class ctrlGeneric {
    
      private static Session sessionHibernate;
        
    public static <T> boolean createItem(T _entity)
    {
        try {
            sessionHibernate = HibernateUtil.getSessionFactory().getCurrentSession();
            sessionHibernate.beginTransaction();
            sessionHibernate.save(_entity);
            sessionHibernate.getTransaction().commit();
            
            return true;
        } catch (Exception e) {
            throw e;
        }finally
        {            
            System.gc();
        }
    }
    
    public static <T> boolean destroitItem(T _entity)
    {
        try {
            sessionHibernate  = HibernateUtil.getSessionFactory().getCurrentSession();
            sessionHibernate.beginTransaction();
            sessionHibernate.delete(_entity);
            sessionHibernate.getTransaction().commit();
            
            return true;
        } catch (Exception e) {
            throw e;
        }finally
        {
            System.gc();
        }
    }
    
    public static <T> boolean updateItem(T _entity)
    {
        try {
            sessionHibernate  = HibernateUtil.getSessionFactory().getCurrentSession();
            sessionHibernate.beginTransaction();
            sessionHibernate.update(_entity);
            sessionHibernate.getTransaction().commit();
            return true;
        } catch (Exception e) {
            throw  e;
        }finally
        {
             System.gc();
        }
    }
    
    public static <T> List<T> selectItems(T _entidad)
    {
        try {
            
            sessionHibernate  = HibernateUtil.getSessionFactory().getCurrentSession();
            sessionHibernate.beginTransaction();
            Criteria lista =  sessionHibernate.createCriteria(_entidad.getClass()); 
            
            return lista.list();
            
        } catch (Exception e) {
            throw e;
        }finally
        {
             System.gc();
        }
    }
    
}
